package org.wutao.supernewsapp.web;

import android.graphics.Bitmap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by vhly[FR].
 * <p>
 * Author: vhly[FR]
 * Email: vhly@163.com
 * Date: 16/9/24
 */

public class MyWebViewClient extends WebViewClient {

    private BrowserSupport mSupport;

    public MyWebViewClient(BrowserSupport support) {
        mSupport = support;
    }

    /**
     * 这个方法用于实现网址的拦截和特定动作的调用
     * 当这个方法返回 false 时, WebView 会自己加载网址
     * 如果这个方法返回 true,那么 WebView 不再进行网址的加载
     * @param view
     * @param url
     * @return
     */
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean ret = false;

        if(!url.startsWith("http://") && !url.startsWith("https://")){
            // TODO: 使用隐式意图 ACTION_VIEW, ACTION_CALL
            // 调用回调接口, 传给外部程序
            if(mSupport != null){
                mSupport.loadAppUrl(view, url);
            }
            ret = true;
        }

        return ret;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (mSupport != null) {
            mSupport.onPageStarted(view, url, favicon);
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (mSupport != null) {
            mSupport.onPageFinished(view, url);
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {

    }

}
